import { createApp } from "vue";
import { createPinia } from "pinia";
import ArcoVue from "@arco-design/web-vue";
import ArcoVueIcon from "@arco-design/web-vue/es/icon";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import directive from "./directive";
// 条件性导入mock数据
if (import.meta.env.VITE_USE_MOCK === "true") {
  import("@/mock");
}
import "@arco-design/web-vue/dist/arco.css";
import "@/assets/style/global.less";
import "@arco-design/web-vue/es/message/style/index.less";
import "@arco-design/web-vue/es/notification/style/index.less";
import "@arco-design/web-vue/es/modal/style/index.less";

import { initVChartArcoTheme } from "@visactor/vchart-arco-theme";

// 初始化图表主题
initVChartArcoTheme();

// 检查是否启用mock
if (import.meta.env.VITE_USE_MOCK === "true") {
  console.log("Mock 数据已启用");
}

const app = createApp(App);

// 添加全局错误处理器
app.config.errorHandler = (err, instance, info) => {
  if (err instanceof Error) {
    console.error("Error message:", err.message);
    console.error("Error stack:", err.stack);
  }

  // 如果是DOM相关的错误，忽略它
  if (err instanceof TypeError && err.message.includes("parentNode")) {
    console.warn("Ignoring DOM-related error during component unmount");
    return;
  }

  // 如果是路由相关的错误，忽略它
  if (
    err instanceof TypeError &&
    err.message.includes("Cannot read properties")
  ) {
    console.warn("Ignoring navigation-related error");
    return;
  }

  // 如果是组件相关的错误，尝试忽略
  if (
    err instanceof Error &&
    (err.message.includes("component") || err.message.includes("render"))
  ) {
    console.warn("Ignoring component-related error");
    return;
  }
};

// 添加全局警告处理器
app.config.warnHandler = (msg, instance, trace) => {
  console.warn("Global warning handler:", msg);
  console.warn("Warning trace:", trace);
  console.warn("Component instance:", instance);
};

app.use(ArcoVue, {});
app.use(ArcoVueIcon);
app.use(createPinia());
app.use(router);
app.use(store);
app.use(directive);

app.mount("#app");
